import { useConfigStore } from "@/stores/config"
import type { TranslateBody } from "@/types/openai"

/**
 * 提供openai的相关api的请求工具集合
 */
export function useOpenaiApi() {

    const configStore = useConfigStore();

    /**
     * 
     * @param {TranslateBody} body 需要翻译的 TranslateBody 对象
     * @returns 
     */
    const translate = async (body: TranslateBody) => {
        const result = await fetch('https://api.openai.com/v1/engines/gpt-4/translate/', {
            method: 'POST',
            headers: {
                "Content-Type": "application/json",
                "Authorization": "Bearer " + configStore.apikey
            },
            body: JSON.stringify(body)
        })
        .then(response => response.json())
        return result;
    }

    return {
        translate
    }
}